package io.zcoder.innoway.activities;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.models.Profile;
import io.zcoder.innoway.models.ResponseBase;
import io.zcoder.innoway.rest.RestClient;
import io.zcoder.innoway.widgets.EditTextCustom;
import io.zcoder.innoway.widgets.TextViewCustom;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class EditInformationActivity extends BaseActivity {

    public static final int REQUEST_EDIT_INFO = 12;

    @BindView(R.id.first_name)
    EditTextCustom mFirstName;
    @BindView(R.id.last_name)
    EditTextCustom mLastName;
    @BindView(R.id.common_save)
    TextViewCustom mSave;
    @BindView(R.id.height)
    TextViewCustom mHeightView;
    @BindView(R.id.age)
    TextViewCustom mAgeView;
    @BindView(R.id.avatar)
    CircleImageView mAvatar;

    @OnClick(R.id.common_save)
    void onSave() {
        if (mFirstName.isEmpty() || mLastName.isEmpty()) {
            showAlert(getString(R.string.error), getString(R.string.error_empty));
            return;
        }
        showProgress();
        RestClient.build(this, true).saveEditInfo(PrefUtils.getInt(this, PrefUtils.PREF_PROFILE_ID),
                mHeight, mUnitHeight, mBirthDay, mFirstName.getText().toString(), mLastName.getText().toString(), mInitWeight, new Callback<ResponseBase>() {
                    @Override
                    public void success(ResponseBase responseBase, Response response) {
                        if (responseBase.getStatus_code() != 200) {
                            showAlert(getString(R.string.error), responseBase.getMessage());
                        } else {
                            finish();
                        }
                        hideProgress();
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        hideProgress();
                    }
                });
    }

    @OnClick(R.id.save)
    void onSaveBtn() {
        onSave();
    }

    @OnClick(R.id.height)
    void editHeight() {
        Intent intent = new Intent(this, SelectTallActivity.class);
        intent.putExtra(Constants.EDIT_INFO_EXTRAS, true);
        if (mProfile != null) {
            intent.putExtra(Constants.PROFILE_EXTRAS, mProfile);
        }
        startActivityForResult(intent, REQUEST_EDIT_INFO);
    }

    @OnClick(R.id.age)
    void editAge() {
        Intent intent = new Intent(this, SelectBirthDayActivity.class);
        intent.putExtra(Constants.EDIT_INFO_EXTRAS, true);
        if (mProfile != null) {
            intent.putExtra(Constants.BIRTHDAY_EXTRAS, mProfile.getBirthday());
        }
        startActivityForResult(intent, REQUEST_EDIT_INFO);
    }

    private Profile mProfile;
    private String mBirthDay;
    private float mHeight;
    private float mInitWeight;
    private int mUnitHeight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_edit_info_activity);
        ButterKnife.bind(this);
        setTitle(getString(R.string.edit_info));
        showBack(true);
        mSave.setVisibility(View.VISIBLE);
        mProfile = getIntent().getParcelableExtra(Constants.PROFILE_EXTRAS);

        fillData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_EDIT_INFO && resultCode == SelectTallActivity.RESULT_SET_TALL) {
            mUnitHeight = data.getIntExtra(Constants.TYPE_TALL_EXTRAS, 0);
            mHeight = data.getFloatExtra(Constants.TALL_EXTRAS, 0);
            mInitWeight = data.getFloatExtra(Constants.INIT_WEIGHT_EXTRAS, 0);
            if (mUnitHeight == SelectTallActivity.TYPE_CM) {
                mHeightView.setText(mHeight + " " + getString(R.string.cm) +" - "+ mInitWeight + " " + getString(R.string.kg));
            } else {
                mHeightView.setText(Utils.convertToInch(mHeight) + " - " + (mInitWeight * Constants.RATIO_KG_TO_LBS) + " " + getString(R.string.lbs));
            }
        }
        if (requestCode == REQUEST_EDIT_INFO && resultCode == SelectBirthDayActivity.RESULT_SET_BIRTHDAY) {
            mBirthDay = data.getStringExtra(Constants.BIRTHDAY_EXTRAS);
            int age = data.getIntExtra(Constants.AGE_EXTRAS, 0);
            mAgeView.setText(age + "");
        }
    }

    private void fillData() {
        if (mProfile == null) {
            return;
        }
        mBirthDay = mProfile.getBirthday();
        mAgeView.setText(mProfile.getAge() + "");
        mHeight = mProfile.getHeight();
        mInitWeight = mProfile.getInitWeight();
        mUnitHeight = mProfile.getHeight_unit();
        if (mUnitHeight == SelectTallActivity.TYPE_CM) {
            mHeightView.setText(mHeight + " " + getString(R.string.cm) +" - "+ mInitWeight + " " + getString(R.string.kg));
        } else {
            mHeightView.setText(Utils.convertToInch(mHeight) + " - " + (mInitWeight * Constants.RATIO_KG_TO_LBS) + " " + getString(R.string.lbs));
        }
        if (!Utils.isBlank(mProfile.getAvatar_url())) {
            Picasso.with(this).load(mProfile.getAvatar_url()).into(mAvatar);
        }
        mFirstName.setText(mProfile.getFirst_name());
        mLastName.setText(mProfile.getLast_name());
    }
}
